package com.llh.uua.model.dto

import com.llh.uua.model.SysRole
import com.llh.uua.model.SysUser
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

/**
 *
 * Created At 2021/3/17 22:19
 * @author llh
 */
data class AccountDto(private val user: SysUser) : UserDetails {

    private val authoritySet: MutableSet<GrantedAuthority> = mutableSetOf()

    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
        return authoritySet
    }

    fun addAuthoritiesForRole(roleList: List<SysRole>): AccountDto {
        this.authoritySet.addAll(roleList.map { SimpleGrantedAuthority(it.roleName) })
        return this
    }

    fun addAuthorities(authorities: List<GrantedAuthority>): AccountDto {
        this.authoritySet.addAll(authorities)
        return this
    }

    fun addAuthorityForRole(sysRole: SysRole): AccountDto {
        this.authoritySet.add(SimpleGrantedAuthority(sysRole.roleName))
        return this
    }

    fun addAuthority(authority: GrantedAuthority): AccountDto {
        this.authoritySet.add(authority)
        return this
    }

    override fun getPassword(): String {
        return user.password
    }

    override fun getUsername(): String {
        return user.username
    }


    override fun isAccountNonExpired(): Boolean {
        return true
    }

    override fun isAccountNonLocked(): Boolean {
        return true
    }

    override fun isCredentialsNonExpired(): Boolean {
        return true
    }

    override fun isEnabled(): Boolean {
        return true
    }
}
